/*
 * json_tcp_parse.c
 *
 *  Created on: May 7, 2016
 *      Author: Alan Wang
 */
#include "json_helper.h"

#include "../../util/log.h"
#include "../../util/str_util.h"

#define TCP_JSONRPC  "1.0"

cJSON *parse_req_to_get_base(MReqBase *req_base, cJSON *pJson);
char *get_final_error_json_str(MReqBase m_req_base,int err_code, char *debug_msg);
char *get_final_res_json_str(MReqBase m_req_base,cJSON  *pResultObject);
char *get_json_str_of_rep_status(MReqBase m_req_base, int status);
char *get_final_req_json_str(MReqBase m_req_base,cJSON  *pParamObject);
cJSON *parse_base_rep2get_result(char *res_status_json);



int parse_req_to_get_cmd_id(char *json_req){
	if(json_req == NULL){
		return NULL;
	}
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	//parse id
	cJSON *pSubJson = cJSON_GetObjectItem(pJson, "id");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return -1;//parse id error
	}
	int cmd_id = pSubJson->valueint;
	cJSON_Delete(pJson);
	return cmd_id;
}

cJSON *parse_req_to_get_base(MReqBase *req_base, cJSON *pJson){
	LogStr("parse_req_to_get_base!\n");
	if(pJson == NULL){
		return NULL;//parse json error
	}
	//parse id
	cJSON *pSubJson = cJSON_GetObjectItem(pJson, "id");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return -1;//parse id error
	}
	req_base->id = pSubJson->valueint;
	Log("req_base->id = %d\n", req_base->id);

	//parse jsonrpc
	pSubJson = cJSON_GetObjectItem(pJson, "jsonrpc");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse jsonrpc error
	}
	strcpy(req_base->jsonrpc, pSubJson->valuestring);

	//parse method
	pSubJson = cJSON_GetObjectItem(pJson, "method");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse method error
	}
	strcpy(req_base->method, pSubJson->valuestring);
	Log("method = %s\n", req_base->method);

	pSubJson = cJSON_GetObjectItem(pJson, "params");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	return pSubJson;
}


int parse_req_for_deviceMasterId(ReqSendMasterInfo *req_sendMasterInfo, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	cJSON *pParamJson = parse_req_to_get_base(&req_sendMasterInfo->req_base, pJson);
	if(pParamJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	cJSON *pDevMasterIdJson = cJSON_GetObjectItem(pParamJson, "deviceMasterId");
	if(pDevMasterIdJson == NULL){
		LogStr("parse json error\n");
		cJSON_Delete(pJson);
		return NULL;//parse status error
	}
	Log("deviceMasterId = %d\n", pDevMasterIdJson->valueint);
	req_sendMasterInfo->dev_master_id = pDevMasterIdJson->valueint;
	cJSON_Delete(pJson);
	return 1;
}


int parse_req_for_sendWifiInfo(ReqSendWifiInfo *req_sendWifiInfo, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	cJSON *pParamJson = parse_req_to_get_base(&req_sendWifiInfo->req_base, pJson);
	if(pParamJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	//parse deviceMasterId
	cJSON *pMasterIdJson = cJSON_GetObjectItem(pParamJson, "deviceMasterId");
	if(pMasterIdJson == NULL){
		LogStr("parse json error\n");
		cJSON_Delete(pJson);
		return NULL;//parse status error
	}
	Log("deviceMasterId = %d\n", pMasterIdJson->valueint);
	req_sendWifiInfo->dev_master_id = pMasterIdJson->valueint;

	cJSON *pWifiListJson = cJSON_GetObjectItem(pParamJson, "wifiList");
	if(NULL == pWifiListJson) {
		LogStr("parse json: wifiList error\n");
		cJSON_Delete(pJson);
		return NULL;
	}
	int wifi_list_len = cJSON_GetArraySize(pWifiListJson);
	int save_wifi_count = wifi_list_len < MAX_WIFI_LIST_LEN ? wifi_list_len : MAX_WIFI_LIST_LEN;
	req_sendWifiInfo->list_len = save_wifi_count;
	int i = 0;
	for(; i < save_wifi_count; i++) {

        cJSON *pWifiInfoJson = cJSON_GetArrayItem(pWifiListJson, i);
        cJSON *pWifiIdJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiId");
        if(NULL == pWifiIdJson) {
        	LogStr("parse json: wifiId error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        req_sendWifiInfo->wifi_list[i].wifi_id = pWifiIdJson->valueint;

        cJSON *pWifiMasterIdJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiMasterId");
        if(NULL == pWifiMasterIdJson) {
        	LogStr("parse json: wifiMasterId error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        req_sendWifiInfo->wifi_list[i].wifi_matser_id = pWifiMasterIdJson->valueint;

        cJSON *pWifiSSIDJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiSSID");
        if(NULL == pWifiSSIDJson) {
        	LogStr("parse json: wifiSSID error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        stpcpy(req_sendWifiInfo->wifi_list[i].wifi_ssid, pWifiSSIDJson->valuestring);

        cJSON *pWifiPasswordJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiPassword");
        if(NULL == pWifiPasswordJson) {
        	LogStr("parse json: wifiPassword error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        stpcpy(req_sendWifiInfo->wifi_list[i].wifi_password, pWifiPasswordJson->valuestring);

        cJSON *pWifiMacJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiMac");
        if(NULL == pWifiMacJson) {
        	LogStr("parse json: wifiMac error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        stpcpy(req_sendWifiInfo->wifi_list[i].wifi_mac, pWifiMacJson->valuestring);

        cJSON *pWifiLongitudeJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiLongitude");
        if(NULL == pWifiLongitudeJson) {
        	LogStr("parse json: wifiLongitude error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        req_sendWifiInfo->wifi_list[i].wifi_longitude = pWifiLongitudeJson->valuedouble;


        cJSON *pWifiLatitudeJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiLatitude");
        if(NULL == pWifiLatitudeJson) {
        	LogStr("parse json: wifiLatitude error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        req_sendWifiInfo->wifi_list[i].wifi_latitude = pWifiLatitudeJson->valuedouble;

        cJSON *pWifiAreaIdJson = cJSON_GetObjectItem(pWifiInfoJson, "wifiAreaId");
        if(NULL == pWifiAreaIdJson) {
        	LogStr("parse json: wifiAreaId error\n");
        	cJSON_Delete(pJson);
        	return NULL;
        }
        req_sendWifiInfo->wifi_list[i].wifi_area_id = pWifiAreaIdJson->valueint;
        //insert to DB
	}
	cJSON_Delete(pJson);
	return 1;
}


int parse_req_for_modifyDeviceWifi(ReqModifyDeviceWifi *req_modifyDeviceWifi, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	cJSON *pParamJson = parse_req_to_get_base(&req_modifyDeviceWifi->req_base, pJson);
	if(pParamJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	cJSON *pDevMasterIdJson = cJSON_GetObjectItem(pParamJson, "deviceMasterId");
	if(pDevMasterIdJson == NULL){
		LogStr("parse json error\n");
		cJSON_Delete(pJson);
		return NULL;//parse status error
	}
	Log("deviceMasterId = %d\n", pDevMasterIdJson->valueint);
	req_modifyDeviceWifi->dev_master_id = pDevMasterIdJson->valueint;

	cJSON *pWifiSSIDJson = cJSON_GetObjectItem(pParamJson, "wifiSSID");
	if(NULL == pWifiSSIDJson) {
		LogStr("parse json: wifiSSID error\n");
		cJSON_Delete(pJson);
		return NULL;;
	}
	stpcpy(req_modifyDeviceWifi->ssid, pWifiSSIDJson->valuestring);

	cJSON *pWifiPasswordJson = cJSON_GetObjectItem(pParamJson, "wifiPassword");
	if(NULL == pWifiPasswordJson) {
		LogStr("parse json: wifiPassword error\n");
		cJSON_Delete(pJson);
		return NULL;;
	}
	stpcpy(req_modifyDeviceWifi->password, pWifiPasswordJson->valuestring);
	cJSON_Delete(pJson);
	return 1;
}


int parse_req_for_getDeviceUserList(ReqGetDeviceUserList *req_getDeviceUserList, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	cJSON *pParamJson = parse_req_to_get_base(&req_getDeviceUserList->req_base, pJson);
	if(pParamJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	cJSON *pDevMasterIdJson = cJSON_GetObjectItem(pParamJson, "deviceMasterId");
	if(pDevMasterIdJson == NULL){
		LogStr("parse json error\n");
		cJSON_Delete(pJson);
		return NULL;//parse status error
	}
	Log("deviceMasterId = %d\n", pDevMasterIdJson->valueint);
	req_getDeviceUserList->dev_master_id = pDevMasterIdJson->valueint;
	cJSON_Delete(pJson);
	return 1;
}


int parse_req_for_deleteDeviceUser(ReqDeleteDeviceUser *req_deleteDeviceUser, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	cJSON *pParamJson = parse_req_to_get_base(&req_deleteDeviceUser->req_base, pJson);
	if(pParamJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	cJSON *pDevMasterIdJson = cJSON_GetObjectItem(pParamJson, "deviceMasterId");
	if(pDevMasterIdJson == NULL){
		LogStr("parse json error\n");
		cJSON_Delete(pJson);
		return -1;//parse status error
	}
	Log("deviceMasterId = %d\n", pDevMasterIdJson->valueint);
	req_deleteDeviceUser->dev_master_id = pDevMasterIdJson->valueint;


	cJSON *pUserMacJson = cJSON_GetObjectItem(pParamJson, "userMac");
	if(pUserMacJson == NULL){
		LogStr("parse json userMac error\n");
		cJSON_Delete(pJson);
		return NULL;//parse status error
	}
	stpcpy(req_deleteDeviceUser->user_mac, pUserMacJson->valuestring);
	cJSON_Delete(pJson);
	return 1;
}

int parse_req_for_unbindDevice(ReqUnbindDevice *req_unbindDevice, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		LogStr("parse json error\n");
		return NULL;//parse json error
	}
	LogStr("parse json success\n");
	cJSON *pParamJson = parse_req_to_get_base(&req_unbindDevice->req_base, pJson);
	if(pParamJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
	cJSON *pDevMasterIdJson = cJSON_GetObjectItem(pParamJson, "deviceMasterId");
	if(pDevMasterIdJson == NULL){
		LogStr("parse json error\n");
		cJSON_Delete(pJson);
		return -1;//parse status error
	}
	Log("deviceMasterId = %d\n", pDevMasterIdJson->valueint);
	req_unbindDevice->dev_master_id = pDevMasterIdJson->valueint;

	cJSON *pDevMacJson = cJSON_GetObjectItem(pParamJson, "deviceMac");
	if(pDevMacJson == NULL){
		LogStr("parse json deviceMac error\n");
		cJSON_Delete(pJson);
		return NULL;//parse status error
	}
	stpcpy(req_unbindDevice->dev_mac, pDevMacJson->valuestring);
	cJSON_Delete(pJson);
	return 1;
}


int parse_req_for_getDeviceStatus(ReqGetDeviceStatus *req_getDeviceStatus, char *json_req){
	cJSON *pJson = cJSON_Parse(json_req);
	if(pJson == NULL){
		LogStr("parse json error\n");
		return NULL;//parse json error
	}
	LogStr("parse json success\n");
	parse_req_to_get_base(&req_getDeviceStatus->req_base, pJson);
	cJSON_Delete(pJson);
	return 1;
}



/**
 * get the final response json string
 */
char *get_final_error_json_str(MReqBase m_req_base,int err_code, char *debug_msg){
	cJSON  *pJsonRoot = NULL;
	pJsonRoot = cJSON_CreateObject();
	if(pJsonRoot == NULL){
		return NULL;
	}
	cJSON_AddNumberToObject(pJsonRoot, "id", m_req_base.id);
	cJSON_AddStringToObject(pJsonRoot, "method", m_req_base.method);
	cJSON_AddStringToObject(pJsonRoot, "jsonrpc", m_req_base.jsonrpc);

	cJSON *errJsonObject = cJSON_CreateObject();;
	cJSON_AddNumberToObject(errJsonObject, "code", err_code);
	cJSON_AddStringToObject(errJsonObject, "debugMsg", debug_msg);
	cJSON_AddItemToObject(pJsonRoot, "error", errJsonObject);

	char  *p_json_str = cJSON_Print(pJsonRoot);
	if(NULL == p_json_str){
		cJSON_Delete(pJsonRoot);
		cJSON_Delete(errJsonObject);
		return NULL;
	}
	cJSON_Delete(pJsonRoot);
//	cJSON_Delete(pResultObject);
//	free(m_req_base);
	return p_json_str;
}


/**
 * get the final response json string
 */
char *get_final_res_json_str(MReqBase m_req_base,cJSON  *pResultObject){
	cJSON  *pJsonRoot = NULL;
	pJsonRoot = cJSON_CreateObject();
	if(pJsonRoot == NULL){
		return NULL;
	}
	cJSON_AddNumberToObject(pJsonRoot, "id", m_req_base.id);
	cJSON_AddStringToObject(pJsonRoot, "method", m_req_base.method);
	cJSON_AddStringToObject(pJsonRoot, "jsonrpc", m_req_base.jsonrpc);
	cJSON_AddItemToObject(pJsonRoot, "result", pResultObject);

	char  *p_json_str = cJSON_Print(pJsonRoot);
	if(NULL == p_json_str){
		cJSON_Delete(pJsonRoot);
		cJSON_Delete(pResultObject);
		return NULL;
	}
	cJSON_Delete(pJsonRoot);
//	cJSON_Delete(pResultObject);
//	free(m_req_base);
	return p_json_str;
}

char *get_json_str_of_rep_status(MReqBase m_req_base, int status){
	cJSON  *pResultObject = NULL;
	pResultObject = cJSON_CreateObject();
	if(pResultObject == NULL){
		return NULL;
	}
	cJSON_AddNumberToObject(pResultObject, "status", status);
	return get_final_res_json_str(m_req_base, pResultObject);
}


char *get_json_str_of_rep_sendMasterInfo(int status){
	MReqBase m_rep_base;
	m_rep_base.id = 1;
	strcpy(m_rep_base.method, "sendMasterInfo");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_json_str_of_rep_status(m_rep_base, status);
}

char *get_json_str_of_error_sendMasterInfo(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 1;
	strcpy(m_rep_base.method, "sendMasterInfo");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}

char *get_json_str_of_rep_sendWifiInfo(int status){
	MReqBase m_rep_base;
	m_rep_base.id = 2;
	strcpy(m_rep_base.method, "sendWifiInfo");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_json_str_of_rep_status(m_rep_base, status);
}

char *get_json_str_of_error_sendWifiInfo(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 2;
	strcpy(m_rep_base.method, "sendWifiInfo");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}

char *get_json_str_of_rep_modifyDeviceWifi(int status){
	MReqBase m_rep_base;
	m_rep_base.id = 3;
	strcpy(m_rep_base.method, "modifyDeviceWifi");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_json_str_of_rep_status(m_rep_base, status);
}

char *get_json_str_of_error_modifyDeviceWifi(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 3;
	strcpy(m_rep_base.method, "modifyDeviceWifi");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}

char *get_json_str_of_rep_getDeviceUserList(DeviceUserInfo *dev_usr_list, int count){
	MReqBase m_rep_base;
	m_rep_base.id = 4;
	strcpy(m_rep_base.method, "getDeviceUserList");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);


	cJSON *pParamJson = NULL;
	pParamJson = cJSON_CreateObject();
	if(NULL == pParamJson) {
		return NULL;
	}
	cJSON *pDevUsrArray = cJSON_CreateArray();
	if(NULL == pDevUsrArray) {
		return NULL;
	}

	int i = 0;
	for(; i < count; i++) {
		cJSON *devUsrJosn = cJSON_CreateObject();
		if(NULL == devUsrJosn) {
			return NULL;
		}
		cJSON_AddStringToObject(devUsrJosn, "deviceName", dev_usr_list[i].name);
		cJSON_AddStringToObject(devUsrJosn, "deviceMac", dev_usr_list[i].mac);
		cJSON_AddItemToArray(pDevUsrArray, devUsrJosn);
	}
	cJSON_AddItemToObject(pParamJson, "deviceUserList", pDevUsrArray);

	return get_final_res_json_str(m_rep_base, pParamJson);
}

char *get_json_str_of_error_getDeviceUserList(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 4;
	strcpy(m_rep_base.method, "getDeviceUserList");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}

char *get_json_str_of_rep_deleteDeviceUser(int status){
	MReqBase m_rep_base;
	m_rep_base.id = 5;
	strcpy(m_rep_base.method, "deleteDeviceUser");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_json_str_of_rep_status(m_rep_base, status);
}

char *get_json_str_of_error_deleteDeviceUser(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 5;
	strcpy(m_rep_base.method, "deleteDeviceUser");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}

char *get_json_str_of_rep_unbindDevice(int status){
	MReqBase m_rep_base;
	m_rep_base.id = 6;
	strcpy(m_rep_base.method, "unbindDevice");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_json_str_of_rep_status(m_rep_base, status);
}

char *get_json_str_of_error_unbindDevice(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 6;
	strcpy(m_rep_base.method, "unbindDevice");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}

char *get_json_str_of_rep_getDeviceStatus(DeviceStatus dev_status){
	MReqBase m_rep_base;
	m_rep_base.id = 7;
	strcpy(m_rep_base.method, "getDeviceStatus");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);

	cJSON *pParamJson = NULL;
	pParamJson = cJSON_CreateObject();
	if(NULL == pParamJson) {
		return NULL;
	}
	cJSON_AddNumberToObject(pParamJson, "workStatus", dev_status.work_status);
	cJSON_AddNumberToObject(pParamJson, "netStatus", dev_status.net_status);
	return get_final_res_json_str(m_rep_base, pParamJson);
}

char *get_json_str_of_error_getDeviceStatus(int err_code, char *debug_msg){
	MReqBase m_rep_base;
	m_rep_base.id = 7;
	strcpy(m_rep_base.method, "getDeviceStatus");
	strcpy(m_rep_base.jsonrpc, TCP_JSONRPC);
	return get_final_error_json_str(m_rep_base, err_code, debug_msg);
}



/************************************************************************************************************
 * notify cmd
 ***********************************************************************************************************/

/**
 * get the final json string
 */
char *get_final_req_json_str(MReqBase m_req_base, cJSON  *pParamObject){
	cJSON  *pJsonRoot = NULL;
	pJsonRoot = cJSON_CreateObject();
	if(pJsonRoot == NULL){
		return NULL;
	}
	cJSON_AddNumberToObject(pJsonRoot, "id", m_req_base.id);
	cJSON_AddStringToObject(pJsonRoot, "method", m_req_base.method);
	cJSON_AddStringToObject(pJsonRoot, "jsonrpc", m_req_base.jsonrpc);
	cJSON_AddItemToObject(pJsonRoot, "params", pParamObject);

	char  *p_json_str = cJSON_Print(pJsonRoot);
	if(NULL == p_json_str){
		cJSON_Delete(pJsonRoot);
		return NULL;
	}
	cJSON_Delete(pJsonRoot);
	return p_json_str;
}



/**
 * get the json string of notify app the device state
 */
char *get_json_str_of_notify_device_state(DeviceState device_state){
	MReqBase m_req_base;
	m_req_base.id = 1;
	strcpy(m_req_base.method, "notifyDeviceState");
	strcpy(m_req_base.jsonrpc, TCP_JSONRPC);

	cJSON  *pParamObject = NULL;
	pParamObject = cJSON_CreateObject();
	if(pParamObject == NULL){
		return NULL;
	}
	cJSON_AddNumberToObject(pParamObject, "netSpeed", device_state.net_speed);
	cJSON_AddNumberToObject(pParamObject, "gpm", device_state.gpm);
	cJSON_AddNumberToObject(pParamObject, "userCount", device_state.user_count);

	return get_final_req_json_str(m_req_base, pParamObject);
}

/**
 * get the json string of notify app the net status
 */
char *get_json_str_of_notify_net_status(NetStatus net_status){
	MReqBase m_req_base;
	m_req_base.id = 2;
	strcpy(m_req_base.method, "notifyNetStatus");
	strcpy(m_req_base.jsonrpc, TCP_JSONRPC);

	cJSON  *pParamObject = NULL;
	pParamObject = cJSON_CreateObject();
	if(pParamObject == NULL){
		return NULL;
	}
	cJSON_AddNumberToObject(pParamObject, "netStatus", net_status);

	return get_final_req_json_str(m_req_base, pParamObject);
}



cJSON *parse_base_rep2get_result(char *res_status_json){
	Log("res_status_json = %s\n", res_status_json);
	if(res_status_json == NULL){
		return NULL;
	}
	cJSON *pJson = cJSON_Parse(res_status_json);
	if(pJson == NULL){
		return NULL;//parse json error
	}
	//parse id
	cJSON *pSubJson = cJSON_GetObjectItem(pJson, "id");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return -1;//parse id error
	}
	Log("id = %d\n", pSubJson->valueint);

	//parse method
//	pSubJson = cJSON_GetObjectItem(pJson, "method");
//	if(pSubJson == NULL){
//		cJSON_Delete(pJson);
//		return NULL;//parse method error
//	}
//	Log("method = %s\n", pSubJson->valuestring);

	//parse result
	pSubJson = cJSON_GetObjectItem(pJson, "result");
	if(pSubJson == NULL){
		cJSON_Delete(pJson);
		return NULL;//parse result error
	}
//	cJSON_Delete(pJson);
	return pSubJson;
}



/**
 * get the response status by json
 */
int get_app_res_status_by_json(char *res_status_json){

	//parse result
	cJSON *pSubJson = parse_base_rep2get_result(res_status_json);
	if(pSubJson == NULL){
		cJSON_Delete(pSubJson);
		return -1;//parse result error
	}
	cJSON *pStaJson = cJSON_GetObjectItem(pSubJson, "status");
	if(pStaJson == NULL){
		cJSON_Delete(pSubJson);
		return -1;//parse status error
	}
	Log("status = %d\n", pStaJson->valueint);
	cJSON_Delete(pSubJson);
	return pStaJson->valueint;
}
